/**
 * @author Victor.Wang
 */

package chinachess.mid;

import java.util.LinkedList;

import android.util.Log;
import chinachess.mid.MID_IPiece.PieceColor;
import chinachess.mid.MID_IPiece.PieceStatus;
import chinachess.mid.MID_IPiece.PieceType;

public class MID_ChessBoard {
	
	public static final String TAG = MID_ChessBoard.class.getSimpleName();
	
	public static final int MAX_COL = 9;
	public static final int MAX_ROW = 10;
	public static final int MAX_CHESS_NUM = 32;
	
	/*
	 * define chess id, map to AI chess type
	 */
	private static final int NO_CHESS = 0;
	private static final int BLACK_KING = 1;
	private static final int BLACK_CAR = 2;
	private static final int BLACK_HORSE = 3;
	private static final int BLACK_CANON = 4; 
	private static final int BLACK_BISHOP = 5;
	private static final int BLACK_ELEPHANT = 6;
	private static final int BLACK_PAWN = 7;
	private static final int RED_KING = 8;
	private static final int RED_CAR = 9;
	private static final int RED_HORSE = 10;
	private static final int RED_CANON = 11;
	private static final int RED_BISHOP = 12;
	private static final int RED_ELEPHANT = 13;
	private static final int RED_PAWN = 14;
	
	//all black pieces
	private MID_Piece mBlackKing;
	private MID_Piece mBlackCar1;
	private MID_Piece mBlackCar2;
	private MID_Piece mBlackHorse1;
	private MID_Piece mBlackHorse2;
	private MID_Piece mBlackCanon1;
	private MID_Piece mBlackCanon2;
	private MID_Piece mBlackBishop1;
	private MID_Piece mBlackBishop2;
	private MID_Piece mBlackElephant1;
	private MID_Piece mBlackElephant2;
	private MID_Piece mBlackPawn1;
	private MID_Piece mBlackPawn2;
	private MID_Piece mBlackPawn3;
	private MID_Piece mBlackPawn4;
	private MID_Piece mBlackPawn5;
	//all red pieces
	private MID_Piece mRedKing;
	private MID_Piece mRedCar1;
	private MID_Piece mRedCar2;
	private MID_Piece mRedHorse1;
	private MID_Piece mRedHorse2;
	private MID_Piece mRedCanon1;
	private MID_Piece mRedCanon2;
	private MID_Piece mRedBishop1;
	private MID_Piece mRedBishop2;
	private MID_Piece mRedElephant1;
	private MID_Piece mRedElephant2;
	private MID_Piece mRedPawn1;
	private MID_Piece mRedPawn2;
	private MID_Piece mRedPawn3;
	private MID_Piece mRedPawn4;
	private MID_Piece mRedPawn5;
	
	//private BoardEventListener eventListener;
	private MID_Piece pieceMap[][];
	private SIDE curSide = SIDE.RED;	//当前活动方
	private int[] moves;	//moves[0]->fromX,moves[1]->fromY,moves[2]->toX,moves[3]->toY
	private byte[] board;
	
	private NativeInterface AI = null;
	private LinkedList<ActionRecord> actionList;	//move record
	
	private static MID_ChessBoard self = null;
	
	/**
	 * 代表对战双方
	 */
	public enum SIDE{
		RED,
		BLACK,
	}
	
	/**
	 * 游戏等级
	 * @author Administrator
	 *
	 */
	public enum GAME_LEVEL{
		EASY,
		NORMAL,
		HARD,
	}
	
	/**
	 * 棋局事件监听器
	 * @author Administrator
	 *
	 */
	/*
	public interface BoardEventListener{
		void onGameOver(SIDE winner);
		void onWillKingKing(SIDE killer);
	}
	*/
	
	/**
	 * 表示棋子在棋盘的位置
	 * X：横坐标，从左到右0-8
	 * Y：纵坐标，从上到下0-9
	 */
	public static class PiecePosition {
		public int x = -1;
		public int y = -1;
	}
	
	private class ActionRecord{
		public MID_Piece srcPiece = null;
		public int srcX;
		public int srcY;
		
		public MID_Piece dstPiece = null;
		public int dstX;
		public int dstY;
		
		/**
		 * 活动记录，作为Stack的元素类型，记录走法步骤用于悔棋
		 * @param src
		 * @param srcx
		 * @param srcy
		 * @param dstx
		 * @param dsty
		 * @param dst
		 */
		public ActionRecord(MID_Piece src,int srcx,int srcy, int dstx,int dsty, MID_Piece dst){
			srcPiece = src;
			srcX = srcx;
			srcY = srcy;
			
			dstPiece = dst;
			dstX = dstx;
			dstY = dsty;
		}
	}
	
	/**
	 * MID_ChessBoard Constructor
	 */
	private MID_ChessBoard(){
		AI = new NativeInterface();	//Native interface接口对象
		Log.i(TAG, AI.getAuthorName());
		moves = new int[4];
		board = new byte[MAX_ROW * MAX_COL];
		actionList = new LinkedList<ActionRecord>();	//走法记录队列，用于悔棋
		initChessBoard();
	}
	
	/**
	 * 获取MID_ChessBoard对象，只能有一个实例
	 * @return
	 */
	public static MID_ChessBoard getInstace(){
		if(self == null){
			self = new MID_ChessBoard();
		}
		return self;
	}
	
	/**
	 * 设置棋局事件监听器
	 * @param listener
	 */
	/*
	public void setEventListener(BoardEventListener listener){
		eventListener = listener;
	}
	*/
	
	/**
	 * 清理对象
	 */
	public void release(){
		self = null;
	}
	
	/**
	 * 初始化棋盘和棋子，包括创建对象并设置初始位置。被构造函数调用一次
	 * @return
	 */
	public boolean initChessBoard(){
		pieceMap = new MID_Piece[MAX_ROW][MAX_COL];
		
		createAllPieces();	//create all chess object
		resetChessBoardDefault();	//reset all chess to default position
		return true;
	}
	
	/**
	 * 创建所有棋子对象，设置颜色和类型
	 */
	private void createAllPieces(){
		mBlackKing = new MID_Piece(PieceColor.BLACK, PieceType.KING);
		mBlackCar1 = new MID_Piece(PieceColor.BLACK, PieceType.CAR);
		mBlackCar2 = new MID_Piece(PieceColor.BLACK, PieceType.CAR);
		mBlackHorse1 = new MID_Piece(PieceColor.BLACK, PieceType.HORSE);
		mBlackHorse2 = new MID_Piece(PieceColor.BLACK, PieceType.HORSE);
		mBlackCanon1 = new MID_Piece(PieceColor.BLACK, PieceType.CANON);
		mBlackCanon2 = new MID_Piece(PieceColor.BLACK, PieceType.CANON);
		mBlackBishop1 = new MID_Piece(PieceColor.BLACK, PieceType.BISHOP);
		mBlackBishop2 = new MID_Piece(PieceColor.BLACK, PieceType.BISHOP);
		mBlackElephant1 = new MID_Piece(PieceColor.BLACK, PieceType.ELEPHANT);
		mBlackElephant2 = new MID_Piece(PieceColor.BLACK, PieceType.ELEPHANT);
		mBlackPawn1 = new MID_Piece(PieceColor.BLACK, PieceType.PAWN);
		mBlackPawn2 = new MID_Piece(PieceColor.BLACK, PieceType.PAWN);
		mBlackPawn3 = new MID_Piece(PieceColor.BLACK, PieceType.PAWN);
		mBlackPawn4 = new MID_Piece(PieceColor.BLACK, PieceType.PAWN);
		mBlackPawn5 = new MID_Piece(PieceColor.BLACK, PieceType.PAWN);
		
		mRedKing = new MID_Piece(PieceColor.RED, PieceType.KING);
		mRedCar1 = new MID_Piece(PieceColor.RED, PieceType.CAR);
		mRedCar2 = new MID_Piece(PieceColor.RED, PieceType.CAR);
		mRedHorse1 = new MID_Piece(PieceColor.RED, PieceType.HORSE);
		mRedHorse2 = new MID_Piece(PieceColor.RED, PieceType.HORSE);
		mRedCanon1 = new MID_Piece(PieceColor.RED, PieceType.CANON);
		mRedCanon2 = new MID_Piece(PieceColor.RED, PieceType.CANON);
		mRedBishop1 = new MID_Piece(PieceColor.RED, PieceType.BISHOP);
		mRedBishop2 = new MID_Piece(PieceColor.RED, PieceType.BISHOP);
		mRedElephant1 = new MID_Piece(PieceColor.RED, PieceType.ELEPHANT);
		mRedElephant2 = new MID_Piece(PieceColor.RED, PieceType.ELEPHANT);
		mRedPawn1 = new MID_Piece(PieceColor.RED, PieceType.PAWN);
		mRedPawn2 = new MID_Piece(PieceColor.RED, PieceType.PAWN);
		mRedPawn3 = new MID_Piece(PieceColor.RED, PieceType.PAWN);
		mRedPawn4 = new MID_Piece(PieceColor.RED, PieceType.PAWN);
		mRedPawn5 = new MID_Piece(PieceColor.RED, PieceType.PAWN);
	}
	
	/**
	 * 将所有棋子重置到棋盘起始位置
	 * @return
	 */
	public boolean resetChessBoardDefault(){
		clearBoardPositionMap();
		actionList.clear();	//clear undo record
		
		//black chess
		mBlackKing.setPieceStatus(PieceStatus.LIVE);
		mBlackKing.setPiecePosition(4, 0);
		pieceMap[0][4] = mBlackKing;
		
		mBlackBishop1.setPieceStatus(PieceStatus.LIVE);
		mBlackBishop1.setPiecePosition(5, 0);
		pieceMap[0][5] = mBlackBishop1;
		
		mBlackBishop2.setPieceStatus(PieceStatus.LIVE);
		mBlackBishop2.setPiecePosition(3, 0);
		pieceMap[0][3] = mBlackBishop2;
		
		mBlackElephant1.setPieceStatus(PieceStatus.LIVE);
		mBlackElephant1.setPiecePosition(6, 0);
		pieceMap[0][6] = mBlackElephant1;
		
		mBlackElephant2.setPieceStatus(PieceStatus.LIVE);
		mBlackElephant2.setPiecePosition(2, 0);
		pieceMap[0][2] = mBlackElephant2;
		
		mBlackHorse1.setPieceStatus(PieceStatus.LIVE);
		mBlackHorse1.setPiecePosition(7, 0);
		pieceMap[0][7] = mBlackHorse1;
		
		mBlackHorse2.setPieceStatus(PieceStatus.LIVE);
		mBlackHorse2.setPiecePosition(1, 0);
		pieceMap[0][1] = mBlackHorse2;
		
		mBlackCar1.setPieceStatus(PieceStatus.LIVE);
		mBlackCar1.setPiecePosition(8, 0);
		pieceMap[0][8] = mBlackCar1;
		
		mBlackCar2.setPieceStatus(PieceStatus.LIVE);
		mBlackCar2.setPiecePosition(0, 0);
		pieceMap[0][0] = mBlackCar2;
		
		mBlackCanon1.setPieceStatus(PieceStatus.LIVE);
		mBlackCanon1.setPiecePosition(7, 2);
		pieceMap[2][7] = mBlackCanon1;
		
		mBlackCanon2.setPieceStatus(PieceStatus.LIVE);
		mBlackCanon2.setPiecePosition(1, 2);
		pieceMap[2][1] = mBlackCanon2;
		
		mBlackPawn1.setPieceStatus(PieceStatus.LIVE);
		mBlackPawn1.setPiecePosition(8, 3);
		pieceMap[3][8] = mBlackPawn1;
		
		mBlackPawn2.setPieceStatus(PieceStatus.LIVE);
		mBlackPawn2.setPiecePosition(6, 3);
		pieceMap[3][6] = mBlackPawn2;
		
		mBlackPawn3.setPieceStatus(PieceStatus.LIVE);
		mBlackPawn3.setPiecePosition(4, 3);
		pieceMap[3][4] = mBlackPawn3;
		
		mBlackPawn4.setPieceStatus(PieceStatus.LIVE);
		mBlackPawn4.setPiecePosition(2, 3);
		pieceMap[3][2] = mBlackPawn4;
		
		mBlackPawn5.setPieceStatus(PieceStatus.LIVE);
		mBlackPawn5.setPiecePosition(0, 3);
		pieceMap[3][0] = mBlackPawn5;
		
		//red chess
		mRedKing.setPieceStatus(PieceStatus.LIVE);
		mRedKing.setPiecePosition(4, 9);
		pieceMap[9][4] = mRedKing;
		
		mRedBishop1.setPieceStatus(PieceStatus.LIVE);
		mRedBishop1.setPiecePosition(3, 9);	
		pieceMap[9][3] = mRedBishop1;
		
		mRedBishop2.setPieceStatus(PieceStatus.LIVE);
		mRedBishop2.setPiecePosition(5, 9);
		pieceMap[9][5] = mRedBishop2;
		
		mRedElephant1.setPieceStatus(PieceStatus.LIVE);
		mRedElephant1.setPiecePosition(2, 9);
		pieceMap[9][2] = mRedElephant1;
		
		mRedElephant2.setPieceStatus(PieceStatus.LIVE);
		mRedElephant2.setPiecePosition(6, 9);
		pieceMap[9][6] = mRedElephant2;
		
		mRedHorse1.setPieceStatus(PieceStatus.LIVE);
		mRedHorse1.setPiecePosition(1, 9);
		pieceMap[9][1] = mRedHorse1;
		
		mRedHorse2.setPieceStatus(PieceStatus.LIVE);
		mRedHorse2.setPiecePosition(7, 9);
		pieceMap[9][7] = mRedHorse2;
		
		mRedCar1.setPieceStatus(PieceStatus.LIVE);
		mRedCar1.setPiecePosition(0, 9);
		pieceMap[9][0] = mRedCar1;
		
		mRedCar2.setPieceStatus(PieceStatus.LIVE);
		mRedCar2.setPiecePosition(8, 9);
		pieceMap[9][8] = mRedCar2;
		
		mRedCanon1.setPieceStatus(PieceStatus.LIVE);
		mRedCanon1.setPiecePosition(1, 7);
		pieceMap[7][1] = mRedCanon1;
		
		mRedCanon2.setPieceStatus(PieceStatus.LIVE);
		mRedCanon2.setPiecePosition(7, 7);
		pieceMap[7][7] = mRedCanon2;
		
		mRedPawn1.setPieceStatus(PieceStatus.LIVE);
		mRedPawn1.setPiecePosition(0, 6);
		pieceMap[6][0] = mRedPawn1;
		
		mRedPawn2.setPieceStatus(PieceStatus.LIVE);
		mRedPawn2.setPiecePosition(2, 6);
		pieceMap[6][2] = mRedPawn2;
		
		mRedPawn3.setPieceStatus(PieceStatus.LIVE);
		mRedPawn3.setPiecePosition(4, 6);
		pieceMap[6][4] = mRedPawn3;
		
		mRedPawn4.setPieceStatus(PieceStatus.LIVE);
		mRedPawn4.setPiecePosition(6, 6);
		pieceMap[6][6] = mRedPawn4;
		
		mRedPawn5.setPieceStatus(PieceStatus.LIVE);
		mRedPawn5.setPiecePosition(8, 6);
		pieceMap[6][8] = mRedPawn5;
		
		AI.initBoard();	//reset ai chess board
		setCurSide(SIDE.RED);
		
		return true;
	}
	
	/**
	 * 棋盘位置MAP数组全部清空，即pieceMap数组
	 */
	public void clearBoardPositionMap(){
		int row, col;
		for(row = 0; row < MAX_ROW; row++){
			for(col = 0; col < MAX_COL; col++){
				pieceMap[row][col] = null;
			}
		}
		//AI.clearBoard();
	}
	
	/**
	 * 在棋盘指定位置设置指定对象，用于载入棋局
	 * @param piece
	 * @param x
	 * @param y
	 */
	public void setBoardPosition(MID_IPiece piece){
		pieceMap[piece.getPiecePosition().y][piece.getPiecePosition().x] = (MID_Piece)piece;
	}
	
	/**
	 * 将载入后的棋盘信息同步到AI C代码中
	 */
	public void updateChessBoard(){
		MID_Piece piece;
		for(int row = 0; row < MAX_ROW; row++)
		{
			for(int col = 0; col < MAX_COL; col++)
			{
				piece = pieceMap[row][col];
				if(piece == null)
				{
					board[row * MAX_COL + col] = NO_CHESS;
					continue;
				}else
				{
					if(piece.getPieceStatus() == PieceStatus.DEAD){
						board[row * MAX_COL + col] = NO_CHESS;
						continue;
					}
					switch(piece.getPieceType())
					{
					case KING:
						if(piece.getPieceColor() == PieceColor.BLACK)
							board[row * MAX_COL + col] = BLACK_KING;
						else
							board[row * MAX_COL + col] = RED_KING;
						break;
					case CAR:
						if(piece.getPieceColor() == PieceColor.BLACK)
							board[row * MAX_COL + col] = BLACK_CAR;
						else
							board[row * MAX_COL + col] = RED_CAR;
						break;
					case HORSE:
						if(piece.getPieceColor() == PieceColor.BLACK)
							board[row * MAX_COL + col] = BLACK_HORSE;
						else
							board[row * MAX_COL + col] = RED_HORSE;
						break;
					case CANON:
						if(piece.getPieceColor() == PieceColor.BLACK)
							board[row * MAX_COL + col] = BLACK_CANON;
						else
							board[row * MAX_COL + col] = RED_CANON;
						break;
					case BISHOP:
						if(piece.getPieceColor() == PieceColor.BLACK)
							board[row * MAX_COL + col] = BLACK_BISHOP;
						else
							board[row * MAX_COL + col] = RED_BISHOP;
						break;
					case ELEPHANT:
						if(piece.getPieceColor() == PieceColor.BLACK)
							board[row * MAX_COL + col] = BLACK_ELEPHANT;
						else
							board[row * MAX_COL + col] = RED_ELEPHANT;
						break;
					case PAWN:
						if(piece.getPieceColor() == PieceColor.BLACK)
							board[row * MAX_COL + col] = BLACK_PAWN;
						else
							board[row * MAX_COL + col] = RED_PAWN;
						break;
					default:
						break;
					}
				}
			}
		}

		AI.resetBoard(board);	//同步到AI C代码中
	}
	
	/**
	 * 根据棋盘位置得到对应的棋子对象
	 * @param col
	 * @param row
	 * @return
	 */
	public MID_IPiece getMidPieceByPosition(int col, int row){
		if((col < MAX_COL) && (col >= 0) && (row < MAX_ROW) && (row >= 0)){
			return pieceMap[row][col];
		}
		
		return null;
	}
	
	/**
	 * 将棋子移动到指定位置
	 * @param fromPosition
	 * @param toPosition
	 * @return	true-移动成功
	 * 			false-移动失败
	 */
	public boolean movePieceTo(PiecePosition fromPosition, PiecePosition toPosition){
		MID_Piece srcPiece = (MID_Piece)pieceMap[fromPosition.y][fromPosition.x];
		MID_Piece dstPiece = (MID_Piece)pieceMap[toPosition.y][toPosition.x];
		if(srcPiece == null)
			return false;
		
		int fromY = fromPosition.y;
		int fromX = fromPosition.x;
		int toY = toPosition.y;
		int toX = toPosition.x;
		
		int res = AI.moveChess(fromX, fromY, toX, toY);
		//Log.d(TAG, "fromX=" + fromX + ",fromY=" + fromY + ", toX=" + toX + ",toY" + toY);
		if(res == 0)	//AI move piece fail
		{
			Log.e(TAG, "Ai move chess fail.");
			return false;
		}
		
		//add to history action list
		actionList.add(new ActionRecord(srcPiece, fromX, fromY, toX, toY, dstPiece));
		
		if(dstPiece != null){	//target position have a chess
			//Log.d(TAG, "target position no chess");
			dstPiece.setPieceStatus(PieceStatus.DEAD);
			srcPiece.setPiecePosition(toPosition);
			pieceMap[toY][toX] = srcPiece;
			pieceMap[fromY][fromX] = null;
		}else{
			//Log.d(TAG, "target position have chess color=" + pieceMap[fromY][fromX].getPieceColor() 
			//		+ " type=" + pieceMap[fromY][fromX].getPieceType());
			srcPiece.setPiecePosition(toPosition);
			pieceMap[fromY][fromX] = null;
			pieceMap[toY][toX] = srcPiece;
		}

		/*
		if(getGameStatus() == NativeInterface.WINNER_BLACK){
			eventListener.onGameOver(SIDE.BLACK);
		}else if(getGameStatus() == NativeInterface.WINNER_RED){
			eventListener.onGameOver(SIDE.RED);
		}
		*/
		this.setCurSide(SIDE.BLACK);

		return true;
	}
	
	/**
	 * 电脑计算下一步走法
	 * @param fromPosition
	 * @param toPosition
	 * @return
	 */
	public boolean getNextStep(PiecePosition fromPosition, PiecePosition toPosition)
	{
		if(fromPosition == null || toPosition == null)
			return false;
		
		if(getGameStatus() != NativeInterface.WINNER_NONE)
		{
			//Log.d(TAG, "getNextStep---game is over");
			return false;
		}
		
		int result = 0;
		result = AI.getNextMove(moves);
		if(result == 1){
			fromPosition.x = moves[0];
			fromPosition.y = moves[1];
			toPosition.x = moves[2];
			toPosition.y = moves[3];
			//Log.i(TAG, "get next move true,fromx=" + moves[0] + " fromy=" + moves[1] + " tox=" + moves[2] + " toy=" + moves[3]);
			return true;
		}
		//Log.d(TAG, "get next move false");
		return false;
	}
	
	/**
	 * 获取当前活动方
	 * @return
	 */
	public SIDE getCurSide()
	{
		return this.curSide;
	}
	
	/**
	 * 设置当前活动方
	 * @param side
	 */
	public void setCurSide(SIDE side)
	{
		this.curSide = side;
	}

	/**
	 * 设置游戏难度：简单，一般，困难
	 */
	public void setGameLevel(GAME_LEVEL level){
		switch(level){
		case EASY:
			AI.setGameLevel(1);
			break;
		case NORMAL:
			AI.setGameLevel(2);
			break;
		case HARD:
			AI.setGameLevel(3);
		}
	}
	
	/**
	 * 获取游戏状态
	 * 
	 * @return	0-游戏未结束
	 * 			1-红方胜利，游戏结束
	 * 			-1-黑方胜利，游戏结束
	 */
	public int getGameStatus(){
		//Log.e(TAG, "Current game status is " + AI.getGameStatus());
		return AI.getGameStatus();
	}
	
	/**
	 * 设置游戏状态
	 * @param status	0-游戏未结束
	 * 					1-红方胜利，游戏结束
	 * 					-1-黑方胜利，游戏结束
	 */
	public void setGameStatus(int status)
	{
		AI.setGameStatus(status);
	}
	
	/**
	 * 通过棋子对象，获取对应到C代码中的棋子类型
	 * @param piece
	 * @return
	 */
	private int getChessID(MID_Piece piece){
		if(piece == null)
			return NO_CHESS;
		
		switch(piece.getPieceType()){
		case KING:
			if(piece.getPieceColor() == PieceColor.BLACK)
				return BLACK_KING;
			else
				return RED_KING;
		case CAR:
			if(piece.getPieceColor() == PieceColor.BLACK)
				return BLACK_CAR;
			else
				return RED_CAR;
		case HORSE:
			if(piece.getPieceColor() == PieceColor.BLACK)
				return BLACK_HORSE;
			else
				return RED_HORSE;
		case CANON:
			if(piece.getPieceColor() == PieceColor.BLACK)
				return BLACK_CANON;
			else
				return RED_CANON;
		case BISHOP:
			if(piece.getPieceColor() == PieceColor.BLACK)
				return BLACK_BISHOP;
			else
				return RED_BISHOP;
		case ELEPHANT:
			if(piece.getPieceColor() == PieceColor.BLACK)
				return BLACK_ELEPHANT;
			else
				return RED_ELEPHANT;
		case PAWN:
			if(piece.getPieceColor() == PieceColor.BLACK)
				return BLACK_PAWN;
			else
				return RED_PAWN;
		default:
			break;
		}
		return NO_CHESS;
	}

	/**
	 * 悔棋一次，包括电脑和玩家的一对走法
	 */
	public boolean undoStep(){
		if(actionList.isEmpty() || actionList.size() % 2 != 0){
			return false;
		}
		ActionRecord record1 = actionList.removeLast();
		AI.setChessIdByPosition(record1.srcX, record1.srcY, getChessID(record1.srcPiece));
		AI.setChessIdByPosition(record1.dstX, record1.dstY, getChessID(record1.dstPiece));
		record1.srcPiece.setPiecePosition(record1.srcX, record1.srcY);
		pieceMap[record1.srcY][record1.srcX] = record1.srcPiece;
		pieceMap[record1.dstY][record1.dstX] = record1.dstPiece;

		//record1.srcPiece.setPieceStatus(PieceStatus.LIVE);
		if(record1.dstPiece != null){
			record1.dstPiece.setPieceStatus(PieceStatus.LIVE);
		}
		
		ActionRecord record2 = actionList.removeLast();
		record2.srcPiece.setPiecePosition(record2.srcX, record2.srcY);
		AI.setChessIdByPosition(record2.srcX, record2.srcY, getChessID(record2.srcPiece));
		AI.setChessIdByPosition(record2.dstX, record2.dstY, getChessID(record2.dstPiece));
		pieceMap[record2.srcY][record2.srcX] = record2.srcPiece;
		pieceMap[record2.dstY][record2.dstX] = record2.dstPiece;
		if(record2.dstPiece != null){
			record2.dstPiece.setPieceStatus(PieceStatus.LIVE);
		}
		
		setGameStatus(NativeInterface.WINNER_NONE);	//set game not over
		record1 = null;
		record2 = null;
		
		return true;
	}
}